// pages/project/project.js
import {
  getProject,
  getProjectList,
  collectInnerArticle,
  cancelCollectArticle
} from '../../api/main'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    open: false,
    navList: [],
    contentList: [],
    navSelected: 0,
    windowHeight: 0,
    navScrollTop: 0,
    pageNum: 0,
    selectedId:0,
    hasLogin: app.globalData.hasLogined
  },

  bindToggle: function () {
    const {
      open
    } = this.data
    let x = open
    this.setData({
      open: !open
    })
  },
  navTap: function (e) {
    const {
      id,
      index
    } = e.currentTarget.dataset
    this.setData({
      navSelected: index,
      pageNum: 0,
      selectedId:id
    })
    this.getProjectListX(id, 0)
  },
  getProjectListX(id, pageNum) {
    getProjectList(id, 0).then((res) => {
      if (res && res.data) {
        let data = res.data
        console.log('res', res)
        this.setData({
          contentList: data.datas
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let height = app.globalData.safeWindowHeight
    this.setData({
      windowHeight: height
    })
    getProject().then((res) => {
      if (res) {
        this.setData({
          navList: res.data
        })
        if (res.data.length > 0) {
          let x = res.data[0]
          this.getProjectListX(x.id, 0)
        }

      }
    })
  },
  loadMore: function () {
    const {
      pageNum,
      selectedId
    } = this.data
    this.setData({
      pageNum:pageNum+1
    },()=>{
      this.getProjectListX(selectedId, this.data.pageNum)
    })
  },
  collect: function (e) {
    console.log('e=', e)
    this.setData({
      hasLogin: app.globalData.hasLogined
    })
    const {
      hasLogin,
      contentList
    } = this.data
    const {
      id,
      index,
    } = e.currentTarget.dataset
    if (hasLogin) {
      let x = contentList[index].collect
      console.log('x',x)
      if (!x) {
        collectInnerArticle(id).then((res) => {
          console.log(res)
          if (res) {
            contentList[index].collect = true
            this.setData({
              contentList:contentList
            })
            wx.showToast({
              title: '收藏成功',
            })
          }else{
          }
        })
      } else {
        cancelCollectArticle(id).then((res) => {
          console.log(res)
          if (res) {
            contentList[index].collect = false
            this.setData({
              contentList:contentList
            })
            wx.showToast({
              title: '取消收藏',
              icon:''
            })
          }
        })
      }

    } else {
      wx.showToast({
        title: '请先登录',
      })
      wx.navigateTo({
        url: '../login/login',
        success: function (res) {
          console.log('resx', res)
        }
      })
    }

  },
  showDetail: function (e) {
    console.log('e', e)
    const {
      link
    } = e.currentTarget.dataset
    const url = encodeURIComponent(JSON.stringify(link));
    wx.navigateTo({
      url: `../webview/webview?url=${url}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})